--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type GM_Runner_C
local M = UnLua.Class()

--- 屏幕
local Screen = require("Screen")

-- function M:Initialize(Initializer)
-- end

function M:UserConstructionScript()
    local BP_RunnerClass = UE.UClass.Load("/Script/Engine.Blueprint'/MashRunner/Blueprints/BP_Runner.BP_Runner_C'")
    if BP_RunnerClass then
        self.DefaultPawnClass = BP_RunnerClass
    end

end

function M:ReceiveBeginPlay()
    self.PlayerNumber = 0
    UE.UKismetSystemLibrary.K2_SetTimerDelegate({ self, self.StartGame }, 3)
end

-- function M:ReceiveEndPlay()
-- end

-- function M:ReceiveTick(DeltaSeconds)
-- end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

-- 宣布获胜者
function M:AnnounceWinner(WinnerIndex)

    self.PlayerNumber = self.PlayerNumber + 1

    if self.PlayerNumber == 2 then
        if self.IsRaceOver == false then
            self.IsRaceOver = true
            Screen.Print(string.format("player：%s Is the Winner!!!", self.Winner))
            -- self.AnnouncedWinne:Broadcast()
            UE.UKismetSystemLibrary.K2_SetTimerDelegate({ self, self.RestartLevel }, 5)
        end
    else
        self.Winner = WinnerIndex
    end
end

-- 游戏正式开始
function M:StartGame()
    self.StartRace:Broadcast()
    Screen.Print("开始游戏！！！！")
end

-- 重新开始关卡
function M:RestartLevel()
    local LevelName = UE.UGameplayStatics.GetCurrentLevelName(self)
    UE.UGameplayStatics.OpenLevel(self, LevelName)
end

return M
